<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Info\Mapper;

use App\Info\Model\InfoAdv;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 广告表Mapper类
 */
class InfoAdvMapper extends AbstractMapper
{
    /**
     * @var InfoAdv
     */
    public $model;

    public function assignModel()
    {
        $this->model = InfoAdv::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 广告自增标识编号
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 广告位id
        if (isset($params['ap_id']) && filled($params['ap_id'])) {
            $query->where('ap_id', '=', $params['ap_id']);
        }

        // 广告内容描述
        if (isset($params['adv_title']) && filled($params['adv_title'])) {
            $query->where('adv_title', 'like', '%'.$params['adv_title'].'%');
        }

        // 广告图片
        if (isset($params['up_img']) && filled($params['up_img'])) {
            $query->where('up_img', 'like', '%'.$params['up_img'].'%');
        }

        // 广告内容
        if (isset($params['adv_content']) && filled($params['adv_content'])) {
            $query->where('adv_content', 'like', '%'.$params['adv_content'].'%');
        }

        // 跳转
        if (isset($params['jump_url']) && filled($params['jump_url'])) {
            $query->where('jump_url', 'like', '%'.$params['jump_url'].'%');
        }

        // 广告开始时间
        if (isset($params['adv_start_time']) && filled($params['adv_start_time']) && is_array($params['adv_start_time']) && count($params['adv_start_time']) == 2) {
            $query->whereBetween(
                'adv_start_time',
                [ $params['adv_start_time'][0], $params['adv_start_time'][1] ]
            );
        }

        // 广告结束时间
        if (isset($params['adv_end_time']) && filled($params['adv_end_time']) && is_array($params['adv_end_time']) && count($params['adv_end_time']) == 2) {
            $query->whereBetween(
                'adv_end_time',
                [ $params['adv_end_time'][0], $params['adv_end_time'][1] ]
            );
        }

        // 幻灯片排序
        if (isset($params['order_sort']) && filled($params['order_sort'])) {
            $query->where('order_sort', '=', $params['order_sort']);
        }

        // 广告点击率
        if (isset($params['click_num']) && filled($params['click_num'])) {
            $query->where('click_num', '=', $params['click_num']);
        }

        // 会员购买的广告是否通过审核0未审核1审核已通过2审核未通过
        if (isset($params['is_allow']) && filled($params['is_allow'])) {
            $query->where('is_allow', '=', $params['is_allow']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 创建时间
        if (isset($params['create_time']) && filled($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        // 更新时间
        if (isset($params['update_time']) && filled($params['update_time'])) {
            $query->where('update_time', '=', $params['update_time']);
        }

        return $query;
    }
}